<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <!--      <el-form-item label="所属用户编号" prop="userId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.userId"-->
      <!--          placeholder="请输入所属用户编号"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="关联模型" prop="modelId">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.modelId"-->
      <!--          placeholder="请选择关联模型"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option label="请选择字典生成" value="" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="角色名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入角色名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="角色类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入角色类别"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否公开" prop="publicStatus">
        <el-select
          v-model="queryParams.publicStatus"
          placeholder="请选择是否公开"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_bool_type"
            :key="String(dict.value)"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="创建时间" prop="createTime">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.createTime"-->
      <!--          value-format="YYYY-MM-DD HH:mm:ss"-->
      <!--          type="daterange"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期"-->
      <!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ai:chat-role:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="80" />
      <!--      <el-table-column label="所属用户编号" prop="userId" />-->
      <el-table-column label="角色名称" prop="name" width="130" />
      <el-table-column label="模型平台" prop="platform">
        <template #default="scope">
          <dict-tag :options="ai_platform" :value="scope.row.platform" />
        </template>
      </el-table-column>
      <el-table-column label="关联模型" prop="modelName" />
      <el-table-column label="角色头像" prop="avatar" width="90">
        <template #default="scope">
          <div class="flex items-center">
            <el-image
              v-if="scope.row.avatar"
              :src="s_file_url(scope.row.avatar)"
              fit="contain"
              class="w-25px h-25px"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="角色类别" prop="category" width="110" />
      <el-table-column label="角色描述" prop="description" />
      <el-table-column label="角色设定" prop="systemMessage" />
      <el-table-column label="引用知识库" prop="knowledgeIds" width="100">
        <template #default="scope">
          <span v-if="!scope.row.knowledgeIds || scope.row.knowledgeIds.length === 0">-</span>
          <span v-else>引用 {{ scope.row.knowledgeIds.length }} 个</span>
        </template>
      </el-table-column>
      <el-table-column label="引用工具" prop="toolIds" width="100">
        <template #default="scope">
          <span v-if="!scope.row.toolIds || scope.row.toolIds.length === 0">-</span>
          <span v-else>引用 {{ scope.row.toolIds.length }} 个</span>
        </template>
      </el-table-column>
      <el-table-column label="是否公开" prop="publicStatus" width="100">
        <template #default="scope">
          <dict-tag :options="common_bool_type" :value="scope.row.publicStatus" />
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" width="100" />
      <el-table-column label="状态" prop="status" width="100">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建时间" prop="createTime" width="180px" />-->
      <el-table-column label="操作" width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['ai:chat-role:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['ai:chat-role:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ChatRoleForm ref="editFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { ChatRoleApi, ChatRoleVO } from '@/api/ai/model/chat_role'
import ChatRoleForm from './ChatRoleForm.vue'
import { s_file_url } from '@/utils/lizard'

/** AI 聊天角色 列表 */
defineOptions({ name: 'AiChatRole' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { ai_platform } = useDict(DICT_TYPE.ai_platform)
const { common_bool_type } = useBoolDict(DICT_TYPE.common_bool_type)
const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<ChatRoleVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  userId: undefined,
  modelId: undefined,
  name: undefined,
  category: undefined,
  publicStatus: true,
  status: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 50
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await ChatRoleApi.getChatRolePage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref<InstanceType<typeof ChatRoleForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await ChatRoleApi.deleteChatRole(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
